﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑任务</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link href="../../../layuiadmin/style/mystyle.css" rel="stylesheet" />
    <script src="../../../layuiadmin/artdialog/artDialog.js?skins=chrome"></script>
    <script src="../../../layuiadmin/lib/jquery.min.js"></script>
    <script src="../../../layuiadmin/lib/api.js"></script>
    <script src="../../../layuiadmin/lib/common.js"></script>
    <!--编辑器-->
    <link href="../../../layuiadmin/kindeditor/themes/default/default.css" rel="stylesheet" />
    <script src="../../../layuiadmin/kindeditor/kindeditor-all.js"></script>
    <script src="../../../layuiadmin/kindeditor/lang/zh-CN.js"></script>
    <script src="../../../layuiadmin/layui/layui.js"></script>
    <script src="../../../layuiadmin/lib/system.js"></script>
    <script>
        //检查服务器是否已登录，没有登录则跳转至登录页。
        sys_islogin();
    </script>
</head>
<body>

    <div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
        <div style="margin-bottom: 45px;">
            <input type="hidden" id="ID" name="ID" value="" />
            <input type="hidden" id="ATTACHCODE" name="ATTACHCODE" value="" />
            <input type="hidden" id="TASKSTATUS" name="TASKSTATUS" value="0" />
            <input type="hidden" name="PROJECTNAME" />
            <div class="layui-form-item">
                <label class="layui-form-label">任务标题</label>
                <div class="layui-input-inline">
                    <input style="width:450px;" type="text" name="TASKNAME" id="TASKNAME" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">任务负责人</label>
                <div class="layui-input-inline">
                    <input style="width:250px;cursor:pointer;" id="LEADERNAME" lay-verify="required" type="text" readonly name="LEADERNAME" placeholder="请选择" autocomplete="off" class="layui-input">
                    <input type="hidden" id="TASKLEADER" name="TASKLEADER" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">任务成员</label>
                <div class="layui-input-inline">
                    <input style="width:250px;cursor:pointer;" id="MEMBERNAME" type="text" name="MEMBERNAME" readonly placeholder="请选择" autocomplete="off" class="layui-input">
                    <input type="hidden" id="TASKMEMBER" name="TASKMEMBER" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">任务审批人</label>
                <div class="layui-input-inline">
                    <input style="width:250px;cursor:pointer;" id="APPROVALNAME" type="text" name="APPROVALNAME" readonly placeholder="请选择" autocomplete="off" class="layui-input">
                    <input type="hidden" id="APPROVALUSER" name="APPROVALUSER" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">任务等级</label>
                <div class="layui-input-inline">
                    <select name="TASKLEVEL" id="TASKLEVEL" lay-verify="">
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">任务标签</label>
                <div class="" id="tip_html">

                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">选择日期</label>
                    <div class="layui-input-inline">
                        <input type="text" readonly class="layui-input" id="TASKSTARTTIME" name="TASKSTARTTIME" placeholder="开始日期">
                    </div>
                    <div class="layui-form-mid">
                        -
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" readonly class="layui-input" id="TASKENDTIME" name="TASKENDTIME" placeholder="结束日期">
                    </div>
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">关联项目</label>
                <div class="layui-input-inline">
                    <select name="PROJECTID" id="PROJECTID" lay-verify="">
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上传文件</label>
                <div class="layui-input-inline" style="width:80%;">
                    <button type="button" class="layui-btn" id="file-upload"><i class="layui-icon"></i>上传文件</button>
                    <ul id="filelist" class="list-inline" style="margin-top:10px;">
                    </ul>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">任务简介</label>
                <div>
                    <textarea name="TASKDESC" id="TASKDESC" style=" height: 400px;width:80%;" autocomplete="off" class="layui-textarea"></textarea>
                </div>
            </div>
        </div>


        <div class="layui-form-item layui-layout-admin" style="text-align:center;">
            <div class="layui-footer-new" style="background-color:#fff;">
                <button class="layui-btn" lay-submit lay-filter="save" id="save">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="close">取消</button>
            </div>
        </div>
    </div>

    
    <script>
        //编辑器
        var editor;
        KindEditor.ready(function (K) {
            editor = K.create('textarea[name="TASKDESC"]', {
                allowImageUpload: true,
                uploadJson: '/KindEditorMerber/UpLoadFiles',
                fileManagerJson: '/KindEditorMerber/FileManager',
                items: [
                    'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                    'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                    'insertunorderedlist', '|', 'emoticons', 'image', 'link']
            });

        });
        layui.config({
            base: '../../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form', 'laydate', 'upload'], function () {
            var $ = layui.$
                , form = layui.form, upload = layui.upload;

            //监听提交
            form.on('submit(save)', function (data) {
                var field = data.field; //获取提交的字段
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                //console.log(field);
                if (field.TASKSTARTTIME > field.TASKENDTIME) {
                    layer.msg("开始时间不能大于结束时间，请重新选择");
                    return;
                }
                data.field.TASKDESC = editor.html(); //获取编辑框的值
                
                var task_tip = common.selectvaluebyName('TASKTIP', 'noTrim');
                console.log(task_tip);
                console.log(data.field.TASKTIP);
                data.field.TASKTIP = task_tip ? ',' + task_tip + ',' : "";
                data.field.PROJECTNAME = $("#PROJECTID option:selected").text();
                //检查任务
                $.ajax({
                    url: "/TaskMerber/CheckTaskByTime",    //请求的url地址
                    dataType: "json",   //返回格式为json
                    //async: false,//请求是否异步，默认为异步，这也是ajax重要特性
                    data: { arg: JSON.stringify(field) },    //参数值
                    type: "POST",   //请求方式                
                    success: function (data) {

                        if (data.count > 0) {
                            var html = "";
                            for (var i = 0; i < data.list.length; i++) {
                                html += data.list[i].TASKSTARTTIME + "至" + data.list[i].TASKENDTIME + ",";
                            }
                            html = common.SubStringEnd(html);
                            layer.confirm('该用户在' + html + '时间已安排工作，确定新增任务吗？', function (index) {
                                checktaskbytime(field, index);
                            });
                        }
                        else
                            checktaskbytime(field, index);
                    },
                    error: function (res) {
                        console.log(res);
                    }
                });

                

            });

            //选择任务负责人
            $('#LEADERNAME').on('click', function () {
                layer.open({
                    type: 2
                    , title: '选择任务负责人'
                    , content: '../../user/user/userselect.html'
                    , maxmin: true
                    , area: ['600px', '480px']
                    , success: function (layero, index) {
                        var iframe = window['layui-layer-iframe' + index];
                        // 向子页面的全局函数child_select_user传参
                        iframe.child_select_user("LEADERNAME", "TASKLEADER", $("#TASKLEADER").val());
                    }
                });
            });

            //选择任务成员
            $('#MEMBERNAME').on('click', function () {
                layer.open({
                    type: 2
                    , title: '选择任务成员'
                    , content: '../../user/user/userselect.html'
                    , maxmin: true
                    , area: ['600px', '480px']

                    , success: function (layero, index) {
                        var iframe = window['layui-layer-iframe' + index];
                        // 向子页面的全局函数child_select_user传参
                        iframe.child_select_user("MEMBERNAME", "TASKMEMBER", $("#TASKMEMBER").val());
                    }
                });
            });

            //选择审批用户
            $('#APPROVALNAME').on('click', function () {
                layer.open({
                    type: 2
                    , title: '选择任务成员'
                    , content: '../../user/user/userselect.html'
                    , maxmin: true
                    , area: ['600px', '480px']

                    , success: function (layero, index) {
                        var iframe = window['layui-layer-iframe' + index];
                        // 向子页面的全局函数child_select_user传参
                        iframe.child_select_user("APPROVALNAME", "APPROVALUSER", $("#APPROVALUSER").val());
                    }
                });
            });

            var laydate = layui.laydate;
            //开始日期
            var insStart = laydate.render({
                elem: '#TASKSTARTTIME'
                //, min: 0
                , type: 'datetime'

                , done: function (value, date) {
                    //更新结束日期的最小日期
                    insEnd.config.min = lay.extend({}, date, {
                        month: date.month - 1
                    });

                    //自动弹出结束日期的选择器
                    insEnd.config.elem[0].focus();
                }
            });

            //结束日期
            var insEnd = laydate.render({
                elem: '#TASKENDTIME'
               // , min: 0
                , type: 'datetime'
                , done: function (value, date) {
                    //更新开始日期的最大日期
                    insStart.config.max = lay.extend({}, date, {
                        month: date.month - 1
                    });
                }
            });

            //上传文件
            var uploadInst = upload.render({
                elem: '#file-upload'
                , url: '/FileMerber/UpLoadFiles'
                , accept: 'file'
                , multiple: true
                , done: function (res) {

                    if (res.flat == 1) {
                        //上传成功,修改数据库的数据
                        if (!$("#ATTACHCODE").val()) $("#ATTACHCODE").val(common.newGuid(32));
                        $.ajax({
                            url: "/FileMerber/updateFile",    //请求的url地址
                            dataType: "json",   //返回格式为json
                            async: false,//请求是否异步，默认为异步，这也是ajax重要特性$("#attachCode").val()
                            data: { arg: JSON.stringify(res.list), attachcode: $("#ATTACHCODE").val(), operation: 'update' },    //参数值
                            type: "POST",   //请求方式
                            success: function (res) {

                                if (res.result > 0) {
                                    var html = "";
                                    for (var i = 0; i < res.list.length; i++) {
                                        html += "<li id=\"" + res.list[i].id + "\"  style=\"height:30px;margin-bottom:10px;\">";
                                        html += "<div class=\"span3\" style=\"font-size: 20px;\">" + res.list[i].filename;
                                        html += "<button onclick=\"deluploadfile('" + res.list[i].id + "')\" class=\"layui-btn layui-btn-normal layui-btn-sm\"' style='float:right;'><i class=\"layui-icon\">&#xe640;</i></button>";
                                        html += "<a target='_blank' href=\"" + res.list[i].filepath + "\" class=\"layui-btn layui-btn-normal layui-btn-sm\"' style='float:right;margin-right:10px;'><i class=\"layui-icon\">&#xe601;</i></a>";
                                        html += "</div>";
                                        html += "</li>";
                                    }

                                    $("#filelist").append(html);
                                }
                                else layer.msg("文件上传失败，请稍后重试");
                            },
                            error: function (res) {
                                console.log(res);
                                layer.msg("上传失败，请联系技术人员");
                            }
                        });

                    }
                    else {
                        //上传失败
                        layer.msg(res.message);
                    }
                }
                , error: function (res) {
                    console.log(res);
                    layer.msg("上传失败，请联系技术人员");
                }
            });

        })

        //删除上传文件div
        var deluploadfile = function (id) {
            layer.confirm('确定删除文件吗？', function (index) {
                $.ajax({
                    url: "/FileMerber/DeleteFile",    //请求的url地址
                    dataType: "json",   //返回格式为json
                    async: false,//请求是否异步，默认为异步，这也是ajax重要特性$("#attachCode").val()
                    data: { id: id },    //参数值
                    type: "POST",   //请求方式
                    success: function (res) {
                        if (res.result > 0) {
                            $('#' + id).remove();
                        }
                        else {
                            layer.msg("文件删除失败，请稍后重试");
                        }
                    },

                    error: function (res) {
                        console.log(res);
                        layer.msg("文件删除失败，请联系技术人员");
                    }
                });
                layer.close(index);
            });
        };

        //接收父页面的数据
        function child_fill_data(data, res) {

            var level_html = "";
            var tip_html = "";
            for (var i = 0; i < res.length; i++) {
                if (res[i].type == 2) {
                    level_html += "<option value='" + res[i].code + "'>" + res[i].codename + "</option>";
                }
                else if (res[i].type == 3) {
                    tip_html += '<input type="checkbox" name="TASKTIP" id="TASKTIP" lay-skin="primary" title="' + res[i].codename + '" value="' + res[i].code + '">';
                }
            }

            $("#TASKLEVEL").html(level_html);
            $("#tip_html").html(tip_html);

            //console.log(data);
            for (let key in data) {
                if (key.toUpperCase() == 'TASKDESC') { //给编辑框赋值
                    editor.html(data[key]);
                }
                else if (key.toUpperCase() == 'TASKTIP') {
                    //console.log();
                    //设置选中
                    common.setcheckboxselect("TASKTIP", data[key]);
                }
                else
                    $("#" + key.toUpperCase()).val(data[key]);
            }

            

            //$("#TASKSTARTTIME").val(common.SubString($("#TASKSTARTTIME").val(), 0, 10));
            //$("#TASKENDTIME").val(common.SubString($("#TASKENDTIME").val(), 0, 10));
            if ($("#ATTACHCODE").val()) {
                $.ajax({
                    url: "/FileMerber/GetFileAttachCode",    //请求的url地址
                    dataType: "json",   //返回格式为json
                    async: false,//请求是否异步，默认为异步，这也是ajax重要特性$("#attachCode").val()
                    data: { attachCode: $("#ATTACHCODE").val()},    //参数值
                    type: "POST",   //请求方式
                    success: function (res) {
                        if (res.flag > 0) {
                            if (res.list.length > 0) {
                                var html = "";
                                for (var i = 0; i < res.list.length; i++) {
                                    html += "<li id=\"" + res.list[i].id + "\"  style=\"height:30px;margin-bottom:10px;\">";
                                    html += "<div class=\"span3\" style=\"font-size: 20px;\">" + res.list[i].filename;
                                    html += "<button onclick=\"deluploadfile('" + res.list[i].id + "')\" class=\"layui-btn layui-btn-normal layui-btn-sm\"' style='float:right;'><i class=\"layui-icon\">&#xe640;</i></button>";
                                    html += "<a target='_blank' href=\"" + res.list[i].filepath + "\" class=\"layui-btn layui-btn-normal layui-btn-sm\"' style='float:right;margin-right:10px;'><i class=\"layui-icon\">&#xe601;</i></a>";
                                    html += "</div>";
                                    html += "</li>";
                                }

                                $("#filelist").append(html);
                            }

                        }
                        else layer.msg("任务附件获取失败，请刷新重试");
                    },
                    error: function (res) {
                        console.log(res);
                        layer.msg("任务附件获取失败，请联系技术人员");
                    }
                });
            }

            $("#PROJECTID").val(data["PROJECTID"]);
        };

        //接收任务的字典
        $(function () {
           
            //获取项目列表
            $.ajax({
                url: "/ProjectMerber/ProjectAllList",    //请求的url地址
                dataType: "json",   //返回格式为json
                async: false,//请求是否异步，默认为异步，这也是ajax重要特性$("#attachCode").val()
                data: {},    //参数值
                type: "POST",   //请求方式
                success: function (res) {
                    //console.log(res);
                    if (res.length > 0) {
                        var html = "<option value=''>请选择关联项目</option>";
                        for (var i = 0; i < res.length; i++) {
                            html += "<option value='" + res[i].ID + "'>" + res[i].PROJECTNAME + "</option>";
                        }
                        $("#PROJECTID").html(html);
                    }
                },
                error: function (res) {
                    console.log(res);
                    //layer.msg("任务附件获取失败，请联系技术人员");
                }
            });
        });

        //关闭弹窗
        $("#close").on("click", function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        });

        //提交任务
        function checktaskbytime(field, index) {

            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                url: "/TaskMerber/UpdateTask",    //请求的url地址
                dataType: "json",   //返回格式为json
                async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                data: { arg: JSON.stringify(field) },    //参数值
                type: "POST",   //请求方式
                beforeSend: function () {
                    //请求前的处理
                },
                success: function (data) {
                    console.log(data);
                    //请求成功时处理
                    if (data > 0) {
                        layer.msg("任务修改成功");
                        parent.layui.table.reload('LAY-app-content-list'); //重载表格
                        parent.layer.close(index); //再执行关闭
                    }
                    else {
                        layer.msg("任务修改失败，请稍后重试");
                    }

                },

                error: function (res) {
                    console.log(res);
                    layer.msg("任务修改失败，请联系技术人员");
                }
            });
        };

    </script>
</body>
</html>